package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import com.google.gson.Gson;
import com.lkm.frame.P;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.LinkBusEventFlag;
import com.lkm.frame.otto.ThreadEnforcer;
import com.lkm.frame.task2.BackCallAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.IOHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.m.steelyardserver.event.OnSteelyardValueEvent;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyarddata.SteelyardValue;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyardstatus.StatusInfo;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsReceiverDataHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;



public class CMDInitValueHandle implements ICMDHandle {

    SteelyardDataModule mSteelyardDataModule;
    SteelyardServerModule mSteelyardServerModule;

    public CMDInitValueHandle() {
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        this.mSteelyardServerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
    }

    

    public static int getAD(int index, byte[] data) {
        if (data.length < 32) {
            return 0;
        }
        int offset = index * 4;
        return NumberHelp.bytesToIntDesc(new byte[]{data[offset], data[offset + 1], data[offset + 2], data[offset + 3]});
    }

    @Override
    public AbsReceiverDataHandle handle(CMDEntity cmdEntity) {

        
        byte[] numByte = NumberHelp.intToBytesDesc(cmdEntity.getNum());
        byte[] datas = CollectionHelp.concatByte(new byte[]{numByte[2], numByte[3]}, cmdEntity.getData());

        if (this.mSteelyardDataModule == null) {
            this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        }

        SteelyardValue steelyardValue = null;
        if(mSteelyardDataModule!=null)
            steelyardValue = mSteelyardDataModule.getSteelyardValue(cmdEntity.getSteelyardMac());

        if(steelyardValue==null)
            steelyardValue = new SteelyardValue();


        steelyardValue.xzfmz = getAD(0, datas);
        steelyardValue.xzfmdynm = getAD(1, datas);
        steelyardValue.lcz = getAD(2, datas);
        steelyardValue.lczdynm = getAD(3, datas);
        steelyardValue.xzkcs = getAD(4, datas);
        steelyardValue.xz0d = getAD(5, datas);
        steelyardValue.zxqp0d = getAD(6, datas);
        steelyardValue.zdqp0d = getAD(7, datas);

        steelyardValue.steelyardMac = cmdEntity.getSteelyardMac();


        SteelyardStatusManagerModule statusManagerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);
        if(statusManagerModule!=null){
            StatusInfo statusInfo = statusManagerModule.getStatusInfoBySteelyardMac(steelyardValue.steelyardMac);
            if (statusInfo!=null)
            steelyardValue.exit_factory_id = statusInfo.getNumno();
        }

        if(P.isDebug()) {
            String json = new Gson().toJson(steelyardValue);
            json = json.replaceAll("\"lcz\"", "\"lcz量程值\"");
            json = json.replace("\"lczdynm\"", "\"lczdynm量程对应内码\"");
            json = json.replace("\"xz0d\"", "\"xz0d校准零点\"");
            json = json.replace("\"xzfmdynm\"", "\"xzfmdynm校准砝码对应内码\"");
            json = json.replace("\"xzfmz\"", "\"xzfmz校准砝码值\"");
            json = json.replace("\"xzkcs\"", "\"xzkcs校准K参数\"");
            json = json.replace("\"zdqp0d\"", "\"zdqp0d最大去皮零点\"");
            json = json.replace("\"zxqp0d\"", "\"zxqp0d最小去皮零点\"");
            P.p("==== steelyardValue " + json);
        }
        if(mSteelyardDataModule!=null)
        mSteelyardDataModule.regSteelyardValue(steelyardValue);

        LinkBus.sendBroadcast(new OnSteelyardValueEvent(steelyardValue));

        


        return null;
    }


    public static class OnTipsEvent implements LinkBusEventFlag{
        public final String tips ;

        public OnTipsEvent(String tips) {
            this.tips = tips;
        }

        @Override
        public ThreadEnforcer getThreadEnforcer() {
            return ThreadEnforcer.ANY;
        }

        @Override
        public boolean isStickyBroadcast() {
            return true;
        }
    }

}
